home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 7 / FM Towns Free Software Collection 7.iso / taropyon / zmodem / src / rsctrl.c < prev    next >
C/C++ Source or Header  |  1993-11-30  |  8KB  |  378 lines

  1. /*    :*****************************************************************
  2.     :    "rsctrl.c" : RS-232C 制御
  3.     :*****************************************************************
  4. */
  5. #include    <stdio.h>
  6. #include    <stdlib.h>
  7. #include    <stdarg.h>
  8. #include    <string.h>
  9. #include    <dos.h>
  10. #include    <msdos.cf>
  11.  
  12. #include    <setupif.h>
  13. #define        _RSCTRL_MAIN
  14. #include    "rsctrl.h"
  15.  
  16. #ifdef    __HIGHC__
  17. #    pragma    On(Align_labels);
  18. #endif
  19.  
  20. #ifndef    MALLOC
  21. #    define    MALLOC    malloc
  22. #endif
  23. #ifndef    CALLOC
  24. #    define    CALLOC    calloc
  25. #endif
  26. #ifndef    FREE
  27. #    define    FREE    free
  28. #endif
  29.  
  30. int        RS_reopen( int port, int md, int bp )
  31. {
  32.     int         er;
  33.     RSB_T    *rsb = RsCtrl[port].rsb;
  34.  
  35.     if ( rsb == NULL )
  36.         return (ERR);
  37.  
  38.     RSB_DTR(port,1);        /*    DTR信号の保持    */
  39.     RSB_CLOSE(port);
  40.  
  41. #ifdef    _RS_FMC
  42.     rsb->mode        = md;                /*    通信モード                */
  43.     rsb->baud        = bp;                /*    ボーレート                */
  44. //    rsb->rbuf        = 0;
  45. //    rsb->rbuf_seg    = 0;
  46.     rsb->stime       = 500;                /*    送信タイムアウト        */
  47.     rsb->rtime       = 500;                /*    受信タイムアウト        */
  48.     rsb->rinfbuf     = 0;                /*    受信通知アドレス        */
  49.     rsb->rinfbuf_seg = 0;
  50.     rsb->extmode     = 0;                /*    拡張モード                */
  51. //    rsb->xon         = 0;                /*    XONコード(0x11)            */
  52. //    rsb->xoff        = 0;                /*    XOFFコード(0x13)        */
  53. //    rsb->obuf        = 0;
  54. //    rsb->obuf_seg    = 0;
  55. #else
  56. #ifdef    _RS_N10
  57.     rsb->mode    = md;                /*    通信モード                */
  58.     rsb->baud    = bp;                /*    ボーレート                */
  59.     rsb->stime   = 0;                /*    送信タイムアウト        */
  60.     rsb->rtime   = 0;                /*    受信タイムアウト        */
  61.     rsb->rinfbuf = 0;                /*    受信通知アドレス        */
  62.     rsb->extmode = 0;                /*    拡張モード                */
  63.     rsb->xon     = 0;                /*    XONコード(0x11)            */
  64.     rsb->xoff    = 0;                /*    XOFFコード(0x13)        */
  65. #endif
  66. #ifdef    _RS_N11
  67.     rsb->mode    = md & (~RSMD_EXINT);    /*    通信モード                */
  68.     rsb->baud    = bp;                    /*    ボーレート                */
  69.     rsb->stime   = 0;                    /*    送信タイムアウト        */
  70.     rsb->rtime   = 0;                    /*    受信タイムアウト        */
  71.     rsb->rinfofs = 0;                    /*    受信通知アドレス        */
  72.     rsb->rinfsel = 0;                    /*    受信通知セレクタ        */
  73.     rsb->extmode = 0;                    /*    拡張モード                */
  74.     rsb->xon     = 0;                    /*    XONコード(0x11)            */
  75.     rsb->xoff    = 0;                    /*    XOFFコード(0x13)        */
  76.     rsb->sendofs = 0;                    /*    送信バッファアドレス    */
  77.     rsb->sendsel = 0;                    /*    送信バッファセレクタ    */
  78. #endif
  79. #endif
  80.     RsCtrl[port].overFlag = 0;
  81.  
  82. #ifdef    _RS_FMC
  83.     if ( (er = RSB_setpara(port, rsb)) == 0 )
  84. #else
  85.     if ( (er = RSB_Setpara(port, (char *)rsb)) == 0 )
  86. #endif
  87.     {
  88.         er = RSB_OPEN(port);
  89.         RSB_INITBUF( port );
  90.     }
  91.  
  92.     return (er);
  93. }
  94.  
  95. void    RS_close(int port)
  96. {
  97.     --RsCtrl[port].use;
  98.     if ( RsCtrl[port].use <= 0 )
  99.     {
  100.         RsCtrl[port].use = 0;
  101.         RSB_CLOSE( port );
  102.     }
  103. }
  104.  
  105. static    int        RsbLimMax = (3*RS_BUFSIZ/4) & 0xFFFFFF00;
  106. static    int        RsbLimMin = (1*RS_BUFSIZ/4) & 0xFFFFFF00;
  107.  
  108. RSB_T    *RS_open( int port )
  109. {
  110.     RSB_T                *rsb;
  111.  
  112.     RS_init();
  113.  
  114.     if ( (rsb = RsCtrl[port].rsb) == NULL )
  115.     {
  116.         if ( (rsb = CALLOC(sizeof(RSB_T),1)) == NULL )
  117.             return (NULL);
  118.         RsCtrl[port].rsb = rsb;
  119.     }
  120. #ifdef    _RS_FMC
  121. #if    0
  122.     {
  123.         RSB_PARA    rsbPara;
  124.         _far void    *ptr;
  125.         size_t        bufsiz;
  126.  
  127.         RSB_rdrpara(port, &rsbPara );
  128.         _FP_SEG(ptr) = rsbPara.rbuf_seg;
  129.         _FP_OFF(ptr) = rsbPara.rbuf;
  130.         bufsiz = *(_far unsigned int *)(ptr);
  131.         RsbLimMax = 3 * bufsiz / 4;
  132.         RsbLimMin = 1 * bufsiz / 4;
  133.     }
  134. #else
  135.         RsbLimMax = 3 * 1024 / 4;
  136.         RsbLimMin = 1 * 1024 / 4;
  137. #endif
  138.     if ( RsCtrl[port].use == 0 )
  139.     {
  140.         RSB_rdrpara(port, rsb );
  141.         RSB_setpara(port, rsb);
  142.         RSB_open(port);
  143.         RsCtrl[port].overFlag = 0;
  144.     }
  145. #else
  146.     /* NATIVE RS */
  147.     {
  148.         RSBUF_T                *rsBuf;
  149.         if ( (rsBuf = RsCtrl[port].rsBuf) == NULL )
  150.         {
  151.             if ( (rsBuf = CALLOC(sizeof(RSBUF_T),1)) == NULL )
  152.                 return (NULL);
  153.             rsBuf->len = RS_BUFSIZ;
  154.             RsCtrl[port].rsBuf = rsBuf;
  155.         }
  156.  
  157.         if ( RsCtrl[port].use == 0 )
  158.         {
  159.             static RSYSINF_T    *rsys = NULL;
  160.  
  161.             if ( rsys == NULL )
  162.             {
  163.                 if ( (rsys = MALLOC(sizeof(RSYSINF_T))) == NULL )
  164.                     return (NULL);
  165.                 ESR_setupInfo(rsys);
  166.             }
  167.             rsb->baud = rsys->rs232c[port].baud;
  168.             rsb->mode = rsys->rs232c[port].mode; /* & (~RSMD_EXINT); */
  169.             rsb->rbuf = (char *)rsBuf;        /*    受信バッファアドレス    */
  170.             rsb->sel  = getds();            /*    セレクター値            */
  171. #ifdef        _RS_N11
  172.             RSB_Setpara(port, (char *)rsb);
  173. #endif
  174.             RSB_OPEN(port);
  175.             RsCtrl[port].overFlag = 0;
  176.         }
  177.     }
  178. #endif
  179.     ++RsCtrl[port].use;
  180.  
  181.     return (rsb);
  182. }
  183.  
  184. int        RS_chk(int port)
  185. {
  186.     int        len;
  187.  
  188.     if ( RsCtrl[port].use == 0 )
  189.         return (ERR);
  190.  
  191.     RSB_READ(port,&len);
  192.     if ( RsCtrl[port].overFlag == FALSE )
  193.     {
  194.         if ( len > RsbLimMax )
  195.         {    RsCtrl[port].overFlag = TRUE;
  196.             RSB_CTRL(port,0x02);
  197.         }
  198.     } else
  199.     {
  200.         if ( len < RsbLimMin )
  201.         {    RsCtrl[port].overFlag = FALSE;
  202.             RSB_CTRL(port,0x22);
  203.         }
  204.     }
  205.     return (len);
  206. }
  207.  
  208. int        RS_getc(int port)
  209. {
  210.     UINT    i;
  211.     UINT    st;
  212.     int        ch;
  213.  
  214.     if ( RsCtrl[port].use == 0 )
  215.         return (ERR);
  216.  
  217. RETRY:
  218.     i = 0;
  219.     LOOP
  220.     {    if ( RSB_RECEIVE(port,&ch,&st) == 0 )
  221.             break;
  222.         if ( ++i > RS_TIMEOUT_LIMIT )
  223.         {    ch = 0;
  224.             break;
  225.         }
  226.     }
  227.     ch &= 0xFF;
  228. #ifdef    _BPLUS_
  229.     if ( ch == 0x05 && (RsCtrl[port].attr & RSATT_BP) )
  230.     {    BP_Term_ENQ(port);
  231.         goto RETRY;
  232.     } else if ( ch == 0x10 && (RsCtrl[port].attr & RSATT_BP) )
  233.     {    BP_DLE_Seen(port);
  234.         goto RETRY;
  235.     }
  236. #endif
  237.     return    (ch);
  238. }
  239.  
  240. void    RS_putc(int port, int ch)
  241. {
  242.     UINT            st;
  243.  
  244.     if ( RsCtrl[port].use == 0 )
  245.         return;
  246.  
  247. #ifdef    _RS_FMC
  248.     RSB_send(port,ch,&st);
  249. #else
  250.     {
  251.         REG UINT        i;
  252.         for ( i = 0 ; i < RS_TIMEOUT_LIMIT; ++i )
  253.         {    if ( RSB_Send(port,ch,&st) == 0 )
  254.                 break;
  255.         }
  256.     }
  257. #endif
  258. }
  259.  
  260. void    RS_puts( int port, CONST char *s )
  261. {
  262.     while ( *s )
  263.     {
  264.         RS_putc( port, (*s) & 0xFF );
  265.         ++s;
  266.     }
  267. }
  268. void    RS_printf( int port, CONST char *form, ... )
  269. {
  270.     va_list        arg;
  271.     char        tmp[BUFSIZ];
  272.  
  273.     va_start( arg, form );
  274.     vsprintf( tmp, form, arg );
  275.     va_end(arg);
  276.     RS_puts( port, tmp );
  277. }
  278.  
  279. void    RS_breakOut( int port, int tm )
  280. {
  281.     if ( RsCtrl[port].use == 0 )
  282.         return;
  283.     RSB_BREAK(port, tm );
  284. }
  285.  
  286. static    char    rsInit = FALSE;
  287.  
  288. /*************************************************************************
  289. *    一時停止処理(子プロセス呼び出しのため)
  290. *-------------------------------------------------------------------------
  291. *    ※    この関数を使った後には,必ずRS_continue を実行すること
  292. *************************************************************************/
  293.  
  294. void    RS_pause(void)
  295. {
  296.     int        port;
  297.  
  298.     if ( rsInit )
  299.     {
  300.         for ( port = 0; port < RS_PORTNUM; ++port )
  301.         {
  302.             if ( RsCtrl[port].use > 0 )
  303.             {
  304.                 if ( RsCtrl[port].overFlag == FALSE )
  305.                 {
  306.                     RsCtrl[port].overFlag = TRUE;
  307.                     RSB_CTRL(port,0x02);
  308.                 }
  309.                 RSB_DTR(port,1);        /*    DTR信号の保持    */
  310.                 RSB_CLOSE(port);
  311.             }
  312.         }
  313. #ifndef    _RS_FMC
  314.         RSB_End();
  315. #endif
  316.     }
  317. }
  318.  
  319. /*************************************************************************
  320. *    再開処理(子プロセス呼び出しのため)
  321. *************************************************************************/
  322. void    RS_continue(void)
  323. {
  324.     int        port;
  325.  
  326.     if ( rsInit )
  327.     {
  328. #ifndef    _RS_FMC
  329.         RSB_Init();
  330. #endif
  331.         for ( port = 0; port < RS_PORTNUM; ++port )
  332.         {
  333.             if ( RsCtrl[port].use > 0 )
  334.             {
  335. #ifdef            _RS_FMC
  336.                 RSB_setpara(port, RsCtrl[port].rsb );
  337. #else
  338.                 RSB_Setpara(port, (char *)RsCtrl[port].rsb );
  339. #endif
  340.                 RSB_OPEN(port);
  341.             }
  342.         }
  343.     }
  344. }
  345.  
  346. int        RS_init(void)
  347. {
  348. #ifdef    _RS_FMC
  349.     return (0);
  350. #else
  351.     if ( rsInit )
  352.         return (NORMAL);
  353.     else
  354.     {    rsInit = TRUE;
  355.         return RSB_Init();
  356.     }
  357. #endif
  358. }
  359.  
  360. int        RS_end(void)
  361. {
  362. #ifdef    _RS_FMC
  363.     return (0);
  364. #else
  365.     if ( rsInit )
  366.     {
  367.         int        port;
  368.         for ( port = 0; port < RS_PORTNUM; ++port )
  369.         {
  370.             while ( RsCtrl[port].use > 0 )
  371.                 RS_close( port );
  372.         }
  373.         return    RSB_End();
  374.     } else
  375.         return (NORMAL);
  376. #endif
  377. }
  378.